package com.android.porttallymansystem.ui.fragment

import android.graphics.Color
import android.os.Binder
import android.os.Bundle
import android.util.Pair
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.amap.api.maps.AMap
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.model.*
import com.amap.api.maps.utils.SpatialRelationUtil
import com.amap.api.maps.utils.overlay.SmoothMoveMarker
import com.android.porttallymansystem.R
import com.android.porttallymansystem.databinding.FragmentMapBinding
import com.android.porttallymansystem.logic.Repository

class MapFragment : Fragment() {

    private lateinit var binding: FragmentMapBinding

    private lateinit var aMap: AMap

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentMapBinding.inflate(inflater, container, false)

        binding.mapView.onCreate(savedInstanceState)

        aMap = binding.mapView.map

        initMap()

        setListener()

        return binding.root
    }

    private fun setListener() {
        binding.switchTrack.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                val points = Repository.getLatLngList()
                if (points.size > 2) {
                    aMap.myLocationStyle = MyLocationStyle().apply {

                        // 取消小蓝点
                        showMyLocation(false)
                        // 只定位一次
                        myLocationType(MyLocationStyle.LOCATION_TYPE_SHOW)

                        val bounds = LatLngBounds(points[0], points[points.size - 2])
                        aMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50))

                        val smoothMarker = SmoothMoveMarker(aMap)
                        // 设置滑动的图标
                        smoothMarker.setDescriptor(BitmapDescriptorFactory.fromResource(R.drawable.ic_point))

                        val drivePoint = points[0]
                        val pair: Pair<Int, LatLng> =
                            SpatialRelationUtil.calShortestDistancePoint(points, drivePoint)
                        points[pair.first] = drivePoint
                        val subList = points.subList(pair.first, points.size)

                        // 设置滑动的轨迹左边点
                        smoothMarker.setPoints(subList)
                        // 设置滑动的总时间
                        smoothMarker.setTotalDuration(3)
                        // 开始滑动
                        smoothMarker.startSmoothMove()
                    }
                    aMap.addPolyline(PolylineOptions().addAll(points).width(10f)
                            .color(Color.argb(255,54,118,249)))
                } else {
                    Toast.makeText(context, "定位数据不足，请稍后重试", Toast.LENGTH_SHORT).show()
                    binding.switchTrack.isChecked = false
                }
            } else {
                initMap()
            }
        }
    }

    // 地图初始化
    private fun initMap() {
        aMap.clear()

        aMap.myLocationStyle = MyLocationStyle().apply {
            showMyLocation(true)
            // 设置连续定位模式下的定位间隔，只在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒。
            interval(5000)
            // 连续定位、且将视角移动到地图中心点，定位点依照设备方向旋转，并且会跟随设备移动
            myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE)

            strokeColor(Color.argb(0, 0, 0, 0))
            radiusFillColor(Color.argb(0, 0, 0, 0))
        }
        aMap.isMyLocationEnabled = true
        aMap.moveCamera(CameraUpdateFactory.zoomTo(15f))

        aMap.uiSettings.apply {
            isZoomControlsEnabled = false
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        binding.mapView.onDestroy()
    }

    override fun onResume() {
        super.onResume()
        binding.mapView.onResume()
    }

    override fun onPause() {
        super.onPause()
        binding.mapView.onPause()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        binding.mapView.onSaveInstanceState(outState)
    }
}